package com.lmy.dp01.service;


import com.lmy.dp01.DAO.StudentRepository;
import com.lmy.dp01.constant.REnum;
import com.lmy.dp01.entity.Student;
import com.lmy.dp01.exception.RException;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class StudentService {

    @Autowired
    public StudentRepository studentRepository;

    /**
     * 读所有的老师列表
     *
     * @return
     */
    public List<Student> getall() {

        return studentRepository.findAll();
    }


    public Page<Student> getAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }

    public Page<Student> getAll(Example<Student> example,Pageable pageable){
        return studentRepository.findAll(example, pageable);
    }




    /**
     * 按照老师ID进行查找
     *
     * @param id 老师ID，是主键
     * @return
     */
    public Student findById(Integer id) {
        return studentRepository.findById(id).orElse(null);
    }

    /**
     * 按照老师名字进行模糊匹配
     *
     * @param name
     * @return
     */
    public List<Student> findByNameLike(String name) {
        return studentRepository.findByNameLike(name);
    }

    /**
     * 新增老师
     *
     * @param student
     */
    public void InsertTeacher(Student student) {
        studentRepository.save(student);
    }

    /**
     * 更新
     *
     * @param student
     */
    public void updateTeacher(Student student) {
        studentRepository.save(student);
    }


    /**
     * 根据id删除老师
     *
     * @param id
     */
    public void deleteTeacher(Integer id) {
        studentRepository.deleteById(id);
    }

    public List<Student> findMaxTno() {
        return studentRepository.findMaxTno();
    }

    /**
     * 新增
     *
     * @param student
     * @return
     */
    public Student add(Student student) {
        return studentRepository.save(student);
    }


    /**
     * 更新
     *
     * @param student
     * @return
     */
    public Student update(Student student) {
        return studentRepository.save(student);
    }


    /**
     * 删除
     *
     * @param id
     */
    public void delete(Integer id) {
        studentRepository.deleteById(id);
    }


    public Student findByName(String name) {
        return studentRepository.findByName(name);
    }


    public Student validateSnoAndPassword(String sno, String password) throws Exception{
        List<Student> students = studentRepository.findBySno(sno);
        if(!students.isEmpty()){
            Student student = students.get(0);
            if (student.getPassword().equals(password)) {
                return student;
            }else {
                throw new RException(REnum.LOGIN_ERR);
            }
        }else {
            throw new RException(REnum.LOGIN_ERR);
        }
    }

    /**
     * 批量删除信息
     * @param ids
     */
    @Transactional(rollbackOn = Exception.class)
    public void deleteByIds(String ids) {
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            studentRepository.deleteById(Integer.parseInt(id));
        }
    }
}
